
package com.intretech.umsin.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intretech.umsin.common.excel.service.ExcelService;
import com.intretech.umsin.common.web.ApiResult;
import com.intretech.umsin.demo.param.OrderParam;
import com.intretech.umsin.demo.service.OrderService;
import com.intretech.umsin.demo.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* 订单:order 对应的API访问类.
* @ClassName: OrderController
* @Description: 订单:order 对应的API访问类.
* @author: 李启联
* @date:   2022-5-28 23:00:30
* @version V1.0
*/
@RestController
@RequestMapping("/demo/order")
@Api(tags = "订单 API")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private ExcelService excelService;

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询单个订单对象", notes = "查询单个订单对象")
    public ApiResult<OrderVo> queryUserById(@PathVariable String id){
        Function<String,OrderVo> fun = p -> orderService.queryById(p);
        return ApiResult.failedResponse().responseFor(fun,id);
    }

    @PostMapping("/listByIds")
    @ApiOperation(value = "根据ID列表查询订单集合对象", notes = "根据ID列表查询订单集合对象")
    public ApiResult<List<OrderVo>> queryUserByIds(@RequestBody List<String> ids){
        Function<List<String>,List<OrderVo>> fun = p -> orderService.queryByIds(p);
        return ApiResult.responseFor(fun,ids);
    }

    @PostMapping("/page/{pageNo}/{pageSize}")
    @ApiOperation(value = "查询返回订单分页对象", notes = "查询返回订单分页对象")
    public ApiResult<IPage<OrderVo>> queryForPage(@RequestBody OrderParam params, @PathVariable Integer pageNo, @PathVariable Integer pageSize){
        BiFunction<IPage,OrderParam,IPage<OrderVo>> fun = (p1, p2) -> orderService.queryForPage(p1, p2);
        return ApiResult.responseFor(fun,new Page(pageNo,pageSize), params);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询订单对象列表", notes = "查询返回订单对象列表")
    public ApiResult<List<OrderVo>> queryForList(@RequestBody OrderParam params){
        Function<OrderParam,List<OrderVo>> fun = p -> orderService.queryForList(p);
        return ApiResult.responseFor(fun,params);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增订单对象", notes = "新增订单对象")
    public ApiResult<OrderVo> add(@RequestBody OrderVo vo){
        Function<OrderVo,OrderVo> fun = p -> orderService.add(p);
        return ApiResult.responseFor(fun,vo);
    }

    @PostMapping("/adds")
    @ApiOperation(value = "新增订单对象列表", notes = "新增订单对象列表")
    public ApiResult<List<OrderVo>> adds(@RequestBody List<OrderVo> voList){
        Function<List<OrderVo>,List<OrderVo>> fun = p -> orderService.adds(p);
        return ApiResult.responseFor(fun,voList);
    }

    @PutMapping("/modify")
    @ApiOperation(value = "更新订单对象", notes = "更新订单对象")
    public ApiResult<OrderVo> modify(@RequestBody OrderVo vo){
        Function<OrderVo,OrderVo> fun = p -> orderService.modify(p);
        return ApiResult.responseFor(fun,vo);
    }

    @PutMapping("/modifys")
    @ApiOperation(value = "更新订单对象", notes = "更新订单对象")
    public ApiResult<List<OrderVo>> modifys(@RequestBody List<OrderVo> voList){
        Function<List<OrderVo>,List<OrderVo>> fun = p-> orderService.modifys(p);
        return ApiResult.responseFor(fun,voList);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除单个订单对象", notes = "删除单个订单对象")
    public ApiResult<Void> removeUser(@PathVariable String id){
        Consumer<String> consumer = p -> orderService.removeById(p);
        return ApiResult.responseFor(consumer,id);
    }

    @DeleteMapping("/removes")
    @ApiOperation(value = "批量删除订单对象", notes = "批量删除订单对象")
    public ApiResult<Void> removeUsers(@RequestBody List<String> ids){
        Consumer<List<String>> consumer = p -> orderService.removeBatchByIds(p);
        return ApiResult.responseFor(consumer, ids);
    }

   @PostMapping("/export")
   @ApiOperation(value = "订单对象导出Excel", notes = "订单对象导出Excel")
   public ApiResult<Void> exportExcel(@RequestBody OrderParam params , HttpServletResponse response) throws Exception {
       Consumer<OrderParam> consumer = p ->{
           try {
               List<OrderVo> ls = orderService.queryForList(p);
               if(!CollectionUtils.isEmpty(ls)){
                   byte[] data = excelService.exportExcel(ls, OrderVo.class,"订单");
                   response.reset();
                   //设置响应头
                   response.setHeader("Content-Disposition", "attachment; filename=\""+ UUID.randomUUID().toString().replace("-","").toUpperCase() +".xls\"");
                   response.addHeader("Content-Length", "" + data.length);
                   response.setContentType("application/msexcel; charset=UTF-8");
                   IOUtils.write(data, response.getOutputStream());
               }
           }catch (Exception e){
                throw new RuntimeException("export excel failed");
           }
       };
       return ApiResult.responseFor(consumer, params);
   }

}